package handler

import (
	"gin-demo/common"
	"gin-demo/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

type AuthHandler interface {
	Login(c *gin.Context)
	Logout(c *gin.Context)
}

type authHandlerImpl struct {
	userService service.UserService
}

func NewAuthHandler(userService service.UserService) AuthHandler {
	return &authHandlerImpl{userService: userService}
}

func (h *authHandlerImpl) Login(c *gin.Context) {
	id := c.Query("id")
	userId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		_ = c.Error(err)
		return
	}
	roles, err := h.userService.GetUserRoles(userId)
	if err != nil {
		_ = c.Error(err)
		return
	}
	token, err := common.GenerateToken(userId, *roles)
	if err != nil {
		_ = c.Error(err)
	}
	c.SetCookie("token", token, 3600, "/", "", false, true)
}

func (h *authHandlerImpl) Logout(c *gin.Context) {

}
